package com.ruoyi.spicfz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.spicfz.domain.Company;
import com.ruoyi.spicfz.domain.Shareholder;
import com.ruoyi.spicfz.service.ICompanyService;
import com.ruoyi.spicfz.service.IShareholderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 股东信息Controller
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@Controller
@RequestMapping("/spicfz/shareholder")
public class ShareholderController extends BaseController
{
    private String prefix = "spicfz/shareholder";

    @Autowired
    private IShareholderService shareholderService;

    @Autowired
    private ICompanyService companyService;

    @RequiresPermissions("spicfz:shareholder:view")
    @GetMapping()
    public String shareholder(ModelMap mmap)
    {
        mmap.put("companyInfos", companyService.selectCompanyList(new Company()));
        return prefix + "/shareholder";
    }

    /**
     * 查询股东信息列表
     */
    @RequiresPermissions("spicfz:shareholder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Shareholder shareholder)
    {
        startPage();
        List<Shareholder> list = shareholderService.selectShareholderList(shareholder);
        return getDataTable(list);
    }

    /**
     * 导出股东信息列表
     */
    @RequiresPermissions("spicfz:shareholder:export")
    @Log(title = "股东信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Shareholder shareholder)
    {
        List<Shareholder> list = shareholderService.selectShareholderList(shareholder);
        ExcelUtil<Shareholder> util = new ExcelUtil<Shareholder>(Shareholder.class);
        return util.exportExcel(list, "股东信息数据");
    }

    /**
     * 新增股东信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //mmap.put("companyInfos", companyService.selectCompanyList(new Company()));
        return prefix + "/add";
    }

    /**
     * 新增保存股东信息
     */
    @RequiresPermissions("spicfz:shareholder:add")
    @Log(title = "股东信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Shareholder shareholder)
    {
        return toAjax(shareholderService.insertShareholder(shareholder));
    }

    /**
     * 修改股东信息
     */
    @GetMapping("/edit/{shareholderId}")
    public String edit(@PathVariable("shareholderId") Long shareholderId, ModelMap mmap)
    {
        //mmap.put("companyInfos", companyService.selectCompanyList(new Company()));
        Shareholder shareholder = shareholderService.selectShareholderById(shareholderId);
        mmap.put("shareholder", shareholder);
        return prefix + "/edit";
    }

    /**
     * 修改保存股东信息
     */
    @RequiresPermissions("spicfz:shareholder:edit")
    @Log(title = "股东信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Shareholder shareholder)
    {
        //System.out.println("实缴金额:"+shareholder.getContributedCapital());
        //System.out.println("图片路径:"+shareholder.getShareholderPath());
        return toAjax(shareholderService.updateShareholder(shareholder));
    }

    /**
     * 删除股东信息
     */
    @RequiresPermissions("spicfz:shareholder:remove")
    @Log(title = "股东信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shareholderService.deleteShareholderByIds(ids));
    }

    /**
     * 查看股东信息
     */
    @RequiresPermissions("spicfz:shareholder:detail")
    @GetMapping("/detail/{shareholderId}")
    public String detail(@PathVariable("shareholderId") Long shareholderId, ModelMap mmap)
    {
        Shareholder shareholder = shareholderService.selectShareholderById(shareholderId);
        mmap.put("shareholder", shareholder);
        mmap.put("detail","true");
        return prefix + "/edit";
    }

}
